<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="商户名称" prop="tenantId">
        <el-select filterable v-model="queryParams.tenantId" clearable placeholder="请输入商户名称" @keyup.enter.native="handleQuery">
          <el-option v-for="tenant in tenants" :key="tenant.id" :label="tenant.name" :value="parseInt(tenant.id)" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.pc_enable_disable_status)" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="small" @click="handleAdd" v-hasPermi="['pekingfish:tenantprofitconfig:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="small" @click="handleExport" :loading="exportLoading" v-hasPermi="['app:tenant-profit-config:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="操作" align="center" fixed="left" class-name="small-padding fixed-width" width="130">
        <template v-slot="scope">
          <el-button size="small" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['pekingfish:tenantprofitconfig:update']">修改</el-button>
        </template>
      </el-table-column>
      <el-table-column label="商户名称" align="center" prop="tenantName" fixed="left" />
      <el-table-column label="分公司名称" align="center" prop="subTenantName" />
      <el-table-column label="商户类型" prop="type" align="center">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.app_account_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="企业分润(%)" align="center" prop="tenantRatio" />
      <el-table-column label="分公司分润(%)" align="center" prop="subTenantRatio" />
      <el-table-column label="店长分润(%)" align="center" prop="shopManagerRatio" />
      <el-table-column label="店员分润(%)" align="center" prop="shopUserRatio" />
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template v-slot="scope">
          <el-switch @change="changeSwitch(scope.row)" v-model="scope.row.status" :active-value="0" :inactive-value="1"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="600px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="账号类型" prop="type">
          <el-select :disabled="form.id?true:false" style="width: 100%;" v-model="form.type" placeholder="请选择账号类型" clearable @change="changeTpye">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.app_account_type)" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
          </el-select>
        </el-form-item>
        <el-form-item label="商户名称" prop="tenantId">
          <el-select :disabled="form.id?true:false" style="width: 100%;" v-model="form.tenantId" clearable placeholder="请输入商户名称" @change="chooseTenant">
            <el-option v-for="tenant in tenants" :key="tenant.id" :label="tenant.name" :value="parseInt(tenant.id)" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.type == 1" label="分公司名称" prop="subTenantId">
          <el-select :disabled="form.id?true:false" style="width: 100%;" v-model="form.subTenantId" clearable placeholder="请选择分公司名称">
            <el-option v-for="subTenant in subTenants" :key="subTenant.id" :label="subTenant.name" :value="parseInt(subTenant.id)" />
          </el-select>
        </el-form-item>
        <el-form-item label="企业分润(%)" prop="tenantRatio">
          <el-input-number style="width: 100%;" v-model="form.tenantRatio" controls-position="right" :min="0" :step="0.01" :precision="2" :max="10000"></el-input-number>
        </el-form-item>
        <el-form-item label="分公司分润(%)" prop="subTenantRatio">
          <el-input-number :disabled="form.type==2?true:false" style="width: 100%;" v-model="form.subTenantRatio" controls-position="right" :min="0" :step="0.01" :precision="2" :max="10000"></el-input-number>
        </el-form-item>
        <el-form-item label="店长分润(%)" prop="shopManagerRatio">
          <el-input-number :disabled="form.type==2?true:false" style="width: 100%;" v-model="form.shopManagerRatio" controls-position="right" :min="0" :step="0.01" :precision="2" :max="10000"></el-input-number>
        </el-form-item>
        <el-form-item label="店员分润(%)" prop="shopUserRatio">
          <el-input-number style="width: 100%;" v-model="form.shopUserRatio" controls-position="right" :min="0" :step="0.01" :precision="2" :max="10000"></el-input-number>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.pc_enable_disable_status)" :key="dict.value" :label="parseInt(dict.value)">{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="loading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createShopDistribution, updateShopDistribution, getShopDistribution, getShopDistributionPage } from "@/api/merchantManagement/shopDistribution";
import { getBusinessList,businessListsAll, subtenantListsAll } from '@/api/merchantManagement/business'
import { getCompanyByType, getBranchCompanyByTenantId } from '@/api/merchantManagement/branchCompany'
export default {
  name: "ShopDistribution",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商户分润配置列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        tenantRatio: null,
        subTenantRatio: null,
        shopManagerRatio: null,
        shopUserRatio: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [{ required: true, message: '账户类型不能为空', trigger: 'change' }],
        tenantId: [{ required: true, message: '请选择商户', trigger: 'change' }],
        tenantRatio: [{ required: true, message: "商户企业分润比例不能为空", trigger: "blur" }],
        subTenantRatio: [{ required: true, message: "分公司分润比例不能为空", trigger: "blur" }],
        shopManagerRatio: [{ required: true, message: "店长分润比例不能为空", trigger: "blur" }],
        shopUserRatio: [{ required: true, message: "店员分润比例不能为空", trigger: "blur" }],
      },
      // 商户列表
      tenants: [],
      // 分公司
      subTenants: [],
    };
  },
  created() {
    this.getList();
    this.getBusinessListsAll()
  },
  methods: {
    // 商户列表
    getBusinessListsAll() {
      businessListsAll().then(response => {
        this.tenants = response.data
      })
    },
    // 分公司名称
    chooseTenant(tenantId) {
      this.form.subTenantId = null;
      this.getsubtenantListsAll(tenantId)
    },
    getsubtenantListsAll(tenantId) {
      subtenantListsAll({tenantId:tenantId}).then(response => {
        this.subTenants = response.data;
      })
    },
    changeTpye() {
      this.form.subTenantId = null;
      this.form.tenantId = null;
      this.getBusinessLists();
    },
    getBusinessLists(val) {
      this.tenants = [];
      businessListsAll({ type: val ? val : this.form.type }).then(response => {
        this.tenants = response.data
      })
      if (this.form.type == 2) {
        this.form.shopManagerRatio = 0;
        this.form.subTenantRatio = 0;
      }
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getShopDistributionPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        subTenantId: undefined,
        tenantId: undefined,
        tenantRatio: 0,
        subTenantRatio: 0,
        shopManagerRatio: 0,
        shopUserRatio: 0,
        status: 0,
        type: 1
      };
      this.resetForm("form");

    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商户分润配置";
      this.getBusinessLists()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = JSON.parse(JSON.stringify(row));
      this.open = true;
      this.title = "修改商户分润配置";
      this.getBusinessLists(row.type)
      if (row.tenantId) {
        this.getsubtenantListsAll(row.tenantId)
      }
    },
    // 修改状态
    changeSwitch(data) {
      updateShopDistribution(data).then(res => {
        this.$message({
          message: '操作成功',
          type: 'success'
        });
        this.getList()
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        if (this.form.type == 2) {
          this.form.shopManagerRatio = 0;
          this.form.subTenantRatio = 0
        }
        // 修改的提交
        this.loading = true;
        if (this.form.id) {
          updateShopDistribution(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
            this.loading = false
          }).catch(f => {
            this.loading = false
          })
        } else {
          // 添加的提交
          createShopDistribution(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
            this.loading = false
          }).catch(f => {
            this.loading = false
          })
        }

      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除商户分润配置编号为"' + id + '"的数据项?').then(function () {
        return deleteShopDistribution(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = { ...this.queryParams };
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有商户分润配置数据项?').then(() => {
        this.exportLoading = true;
        return exportShopDistributionExcel(params);
      }).then(response => {
        this.$download.excel(response, '商户分润配置.xls');
        this.exportLoading = false;
      }).catch(() => { });
    }
  }
};
</script>
<style>
.el-input-number.is-controls-right .el-input__inner {
  text-align: left;
}
</style>